package vn.hanelsoft.newsduet.common;

import android.util.Log;


public class UtilsCommon {

	public static String catTrym(String input, int length) {
		if (input.length() <= length) {
			return input;
		} else {
			String strResult = input.substring(0, length);
			if (!input.substring(length, length+1).equals(" ")) {
				strResult = strResult.substring(0, strResult.lastIndexOf(" "));
			}
			return strResult;
		}
	}

	public static String tachChuoi(String input, String sub) {
		String strResult = "";
		String[] list_str = input.split(sub);
		for (int i = 0; i < list_str.length; i++) {
			strResult += list_str[i];
		}
		return strResult;
	}
	
	public static String subStringText(String input, String str_start, String str_end)
	{
		String str = "";
		int positionStart = input.indexOf(str_start);
//		Log.d("Luong", "Sub img str_start: "+str_start);
//		Log.d("Luong", "Sub img positionStart: "+positionStart);
		if(positionStart >= 0)
		{
			int positionEnd = input.indexOf(str_end, positionStart);
//			Log.d("Luong", "Sub img str_end: "+str_end);
//			Log.d("Luong", "Sub img positionEnd: "+positionEnd);
			if(positionEnd>=0)
			{
				str = input.substring(positionStart+str_start.length(), positionEnd);
			}
//			Log.d("Luong", "Sub img str: "+str);
		}
		return str;
	}

	public static String convertDescriptionToSummary(String input) {
		int start = input.indexOf("<p>")+3;
		int end = input.indexOf("</p>");
		if(start > 0 && start<end)
			return catTrym(input.substring(start, end), 200);
		return "";
	}

	public static String convertDescriptionToImage(String input) {
		String []list_string = input.split("<img src=\"");
		if(list_string.length>1){
			String imageLink = list_string[1];
			list_string = imageLink.split("\"");
			if(list_string.length>0){
				return list_string[0];
			}
		}
		return "";
	}

}
